package ods_map

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"server/global"
	"server/model/common/request"
	"server/model/common/response"
	"strings"
)

type DbFldMapApi struct {
}

var dbFldMapService = DbFldMapService{}


func (dbFldMapApi *DbFldMapApi) CreateDbFldMap(c *gin.Context) {
	var dbFldMap DbFldMap
	err := c.ShouldBindJSON(&dbFldMap)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	dbFldMap.SrcFldType = strings.ToLower(dbFldMap.SrcFldType)
	dbFldMap.TgtFldType = strings.ToLower(dbFldMap.TgtFldType)
	dbFldMap.SrcFldType1 = strings.ToLower(dbFldMap.SrcFldType1)
	dbFldMap.TgtFldType1 = strings.ToLower(dbFldMap.TgtFldType1)

	if err := dbFldMapService.CreateDbFldMap(&dbFldMap); err != nil {
		global.Logger.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}


func (dbFldMapApi *DbFldMapApi) DeleteDbFldMap(c *gin.Context) {
	var dbFldMap DbFldMap
	err := c.ShouldBindJSON(&dbFldMap)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := dbFldMapService.DeleteDbFldMap(dbFldMap); err != nil {
		global.Logger.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}


func (dbFldMapApi *DbFldMapApi) DeleteDbFldMapByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := dbFldMapService.DeleteDbFldMapByIds(IDS); err != nil {
		global.Logger.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}


func (dbFldMapApi *DbFldMapApi) UpdateDbFldMap(c *gin.Context) {
	var dbFldMap DbFldMap
	err := c.ShouldBindJSON(&dbFldMap)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	dbFldMap.SrcFldType = strings.ToLower(dbFldMap.SrcFldType)
	dbFldMap.TgtFldType = strings.ToLower(dbFldMap.TgtFldType)
	dbFldMap.SrcFldType1 = strings.ToLower(dbFldMap.SrcFldType1)
	dbFldMap.TgtFldType1 = strings.ToLower(dbFldMap.TgtFldType1)

	if err := dbFldMapService.UpdateDbFldMap(dbFldMap); err != nil {
		global.Logger.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}


func (dbFldMapApi *DbFldMapApi) FindDbFldMap(c *gin.Context) {
	var dbFldMap DbFldMap
	err := c.ShouldBindQuery(&dbFldMap)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if redbFldMap, err := dbFldMapService.GetDbFldMapById(dbFldMap.ID); err != nil {
		global.Logger.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"redbFldMap": redbFldMap}, c)
	}
}


func (dbFldMapApi *DbFldMapApi) GetDbFldMapList(c *gin.Context) {
	var pageInfo DbFldMapSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := dbFldMapService.GetDbFldMapInfoList(pageInfo); err != nil {
		global.Logger.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}


func (dbFldMapApi *DbFldMapApi) GetAllDbFldMaps(c *gin.Context) {
	dbFldMap, err := dbFldMapService.GetAllDbFldMaps()
	if err != nil {
		global.Logger.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(gin.H{"dbFldMap": dbFldMap}, "获取成功", c)
	}
}


func (dbFldMapApi *DbFldMapApi) GetDbFldMap(c *gin.Context) {
	dbFldMap, err := dbFldMapService.GetDbFldMap()
	if err != nil {
		global.Logger.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(gin.H{"dbFldMap": dbFldMap}, "获取成功", c)
	}
}


func (dbFldMapApi *DbFldMapApi) InitDbFldMap(c *gin.Context) {
	response.FailWithMessage("获取初始化数据失败", c)
}
